package cn.hl.hm.controller.manage;

import cn.hl.hm.dao.CountrySubdivisionCodeMapper;
import cn.hl.hm.entity.CountrySubdivisionCode;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 线路的城市查询
 * Created by xianghan on 18/1/16.
 */
@Controller
@RequestMapping("/manage/routeCity")
public class RouteCityController {
    private static final Logger logger = LoggerFactory.getLogger(RouteCityController.class);
    @Autowired
    private CountrySubdivisionCodeMapper countrySubdivisionCodeMapper;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(@RequestParam(required = false) String provincialCode,
                       HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---线路城市列表---manage/routeCity/list");
            request.setCharacterEncoding("UTF-8");
            List<CountrySubdivisionCode> cscList=null;
            if (provincialCode==null || provincialCode.length()==0) {
                cscList = countrySubdivisionCodeMapper.selectFirstLevel();
            }else{
                provincialCode=provincialCode.substring(0,2);
                cscList = countrySubdivisionCodeMapper.selectByProvincialCode(provincialCode);
            }
            Map<String,Object> data=new HashMap<>();
            data.put("cscList",cscList);
            Result.ok(data, InfoCode.SUCCESS, response);
        } catch (Exception e) {
            Result.ok(null, InfoCode.FAILURE, response);
            e.printStackTrace();
        }
        return null;
    }
}
